<?php

declare(strict_types=1);

namespace App\Command;

use App\Amqp\Producer\OrderProducer;
use App\Model\Order;
use App\Service\order\OrderService;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Hyperf\Amqp\Producer;
use App\Service\ElasticsearchService;
use Hyperf\Elasticsearch\ClientBuilderFactory;

#[Command]
class syncOrderDataToEs extends HyperfCommand

{
    private $producer;
    public function __construct(protected ContainerInterface $container,Producer $producer)
    {
        parent::__construct('sync:order');
        $this->producer = $producer;
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('同步订单表到ES');
    }

    public function handle()
    {
        while(true){
            $data = Order::query()->where('sync', 1)->limit(100)->get();
            if($data->isEmpty()){break;}

            foreach ($data as $key => $dt) {
                $message = new OrderProducer($dt->id);
                $this->producer->produce($message);
                $dt->sync = 0;
                $dt->save();
            }
        }
        
        $this->line('处理完成' , 'info');
    }
}
